package se.webbzon.boltzmann.minigame.effect;

import java.awt.Color;

import se.webbzon.boltzmann.minigame.animation.FadeAnimation;
import se.webbzon.boltzmann.minigame.animation.FloatAnimation;
import se.webbzon.boltzmann.minigame.animation.OrAnimation;
import se.webbzon.boltzmann.minigame.animation.ResizeAnimation;
import se.webbzon.boltzmann.minigame.sandbox.AnimationListener;
import se.webbzon.boltzmann.minigame.sandbox.SandBox;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxAnimation;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxItem;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxPicture;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxText;
import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.font.HorizontalAlignment;

public class SandBoxFeedback {
	
	public enum Symbol {POSITIVE, NEGATIVE, RIGHT, UP, LEFT, DOWN};
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Creates a feedback symbol for the provided sand box by using
	 * the given symbol. **/
	public static SandBoxItem createSymbol(SandBox box, Symbol symbol) {
		final String resourceName;
		switch (symbol) {
		case POSITIVE: resourceName = "minigame_feedback_positive"; break;
		case NEGATIVE: resourceName = "minigame_feedback_negative"; break;
		case RIGHT: resourceName = "minigame_feedback_right"; break;
		case UP: resourceName = "minigame_feedback_up"; break;
		case LEFT: resourceName = "minigame_feedback_left"; break;
		case DOWN: resourceName = "minigame_feedback_down"; break;
		default: resourceName = "minigame_feedback_down"; break;
		}
		SandBoxPicture picture = new SandBoxPicture(Resources.getImageSet(resourceName),2);
		picture.setSize(32, 32);
		SandBoxAnimation ani = new OrAnimation(new FadeAnimation(20), new ResizeAnimation(20,192,192));
		ani.addListener(new ItemDestroyer(box,picture));
		picture.setAnimation(ani);
		return picture;
	}
	
	/** Creates an invocation image for the provided sand box. **/
	public static SandBoxItem createInvocation(SandBox box) {
		SandBoxPicture picture = new SandBoxPicture(Resources.getImageSet("minigame_feedback_event"),2);
		picture.setSize(32, 32);
		SandBoxAnimation ani = new OrAnimation(new FadeAnimation(10), new ResizeAnimation(10,64,64));
		ani.addListener(new ItemDestroyer(box,picture));
		picture.setAnimation(ani);
		return picture;
	}
	
	/** Creates a score text for the provided sand box. **/
	public static SandBoxItem createScore(SandBox box, int points) {
		String prefix = points >= 0 ? "+" : "";
		SandBoxText text = new SandBoxText(prefix + points + "p");
		text.setHorizontalAlignment(HorizontalAlignment.CENTER);
		text.setColor(points >= 0 ? Color.green : Color.red);
		text.setSize(14, 14);
		SandBoxAnimation ani = new FloatAnimation(30,0,-32);
		ani.addListener(new ItemDestroyer(box,text));
		text.setAnimation(ani);
		return text;
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	/** An implementation of an animation listener. **/
	private static final class ItemDestroyer implements AnimationListener {
		
		private final SandBox box;
		private final SandBoxItem item;
		
		public ItemDestroyer(SandBox box, SandBoxItem item) {
			this.box = box;
			this.item = item;
		}

		@Override public void onComplete(SandBoxAnimation animation) {
			box.removeItem(item);
		}
		
	}

}
